package com.siyoumi.app.modules.app_book.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyoumi.app.entity.BookOrder;
import com.siyoumi.app.entity.BookSpu;
import com.siyoumi.app.modules.app_book.entity.EnumBookOrderStatus;
import com.siyoumi.app.modules.app_book.entity.EnumBookSpuUserCount;
import com.siyoumi.app.modules.app_book.service.SvcBookOrder;
import com.siyoumi.app.modules.app_book.service.SvcBookSpu;
import com.siyoumi.app.modules.app_book.service.SvcBookStore;
import com.siyoumi.app.modules.app_book.vo.VaBookOrderCheckin;
import com.siyoumi.app.modules.app_book.vo.VaBookOrderCheckout;
import com.siyoumi.app.modules.app_book.vo.VaBookOrderConfirm;
import com.siyoumi.app.modules.app_book.vo.VoBookSpuAudit;
import com.siyoumi.app.sys.entity.EnumEnable;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XEnumBase;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/app_book/app_book__order__list")
public class app_book__order__list
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("订单列表");

        InputData inputData = InputData.fromRequest();
        String orderType = inputData.input("order_type"); //排序类型
        String excludeDate = inputData.input("exclude_date"); //排除日期条件
        if ("1".equals(excludeDate)) {

        } else {
            inputData.dateSearch(this, "create_begin", "create_end");
        }


        String[] select = {
                "border_id",
                "border_create_date",
                "border_date_begin",
                "border_date_end",
                "border_date_end_final",
                "border_user_name",
                "border_user_phone",
                "border_day",
                "border_day_final",
                "border_set_name",
                "border_spu_count",
                "border_sku_user_count",
                "border_set_user_count",
                "border_deposit",
                "border_deposit_price",
                "border_price_spu",
                "border_price_final",
                "border_price_pay",
                "border_price_pay01",
                "border_price_pay_total",
                "border_price_pay_total_lock",
                "border_status",
                "border_desc",
                "border_refund",
                "border_spu_name",
                "border_spu_pay",
                "border_continue",
                "border_continue_main_id",
                "border_continue_price_total",
                "border_continue_day_total",
                "bspu_name",
        };
        JoinWrapperPlus<BookOrder> query = SvcBookOrder.getBean().listQuery(inputData);
        query.join(BookSpu.table(), BookSpu.tableKey(), "border_spu_id");
        query.eq("border_store_id", getStoreId())
                .eq("border_enable", 1);
        if (XStr.hasAnyText(orderType)) {
            switch (orderType) {
                case "1":
                    query.orderByAsc("border_date_begin")
                            .orderByAsc("border_id");
                    break;
            }
        } else {
            query.orderByDesc("border_create_date")
                    .orderByAsc("border_id");
        }
        query.select(select);

        IPage<BookOrder> page = new Page<>(inputData.getPageIndex(), inputData.getPageSize(), !isAdminExport());
        //list
        IPage<Map<String, Object>> pageData = SvcBookOrder.getApp().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();
        long count = pageData.getTotal();

        EnumBookOrderStatus enumOrderStatus = EnumBookOrderStatus.of(false);
        for (Map<String, Object> item : list) {
            BookOrder entity = XBean.fromMap(item, BookOrder.class);
            item.put("id", entity.getKey());
            //订单日期
            item.put("date_begin", XDate.toDateString(entity.getBorder_date_begin()));
            item.put("date_end", entity.strEndDate(false));
            item.put("date_end_continue", entity.strEndDate(true));
            //状态
            item.put("status", enumOrderStatus.get(entity.getBorder_status()));
            //未付金额
            item.put("pay_price_left", entity.matchPayPriceLeft());
        }

        if (!isAdminExport()) {
            getR().setData("list", list);
            getR().setData("count", count);

            //房源列表
            List<Map<String, Object>> listSpu = SvcBookSpu.getBean().getList(getStoreId());
            setPageInfo("list_spu", listSpu);
            //订单状态
            setPageInfo("order_status", enumOrderStatus);
        } else {
            LinkedMap<String, String> tableHead = new LinkedMap<>();
            tableHead.put("border_id", "订单号");
            tableHead.put("bspu_name", "房源名称");
            tableHead.put("border_spu_count", "预订数量");
            tableHead.put("date_begin", "入住日期");
            tableHead.put("date_end", "退房日期");
            tableHead.put("border_set_user_count", "供餐人数");
            tableHead.put("border_set_name", "供餐信息");
            tableHead.put("border_user_name", "预订人-姓名");
            tableHead.put("border_user_phone", "预订人-手机");
            tableHead.put("border_deposit_price", "押金");
            tableHead.put("border_price_spu", "房费");
            tableHead.put("border_price_final", "总金额");
            tableHead.put("pay_price_left", "未付金额");
            tableHead.put("status", "订单状态");
            tableHead.put("border_desc", "备注");
            tableHead.put("border_create_date", "创建时间");

            List<List<String>> listDataEx = adminExprotData(tableHead, list, pageData.getCurrent() == 1);
            getR().setData("list", listDataEx);
        }

        return getR();
    }

    @GetMapping("/export")
    public XReturn export() {
        setAdminExport();
        return index();
    }

    //订单确认
    @Transactional
    @PostMapping("/confirm")
    public XReturn confirm(@Validated VaBookOrderConfirm vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);

        XReturn r = SvcBookOrder.getBean().confirm(vo);
        if (r.ok()) {
            if (vo.getConfirm() == 10) { //取消订单
                SvcBookOrder.getBean().refundByOrderID(vo.getId());
            }
        }
        return r;
    }

    //订单入住
    @Transactional
    @PostMapping("/checkin")
    public XReturn checkin(@Validated VaBookOrderCheckin vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);

        return SvcBookOrder.getBean().checkin(vo);
    }

    //订单退房
    @Transactional
    @PostMapping("/checkout")
    public XReturn checkout(@Validated VaBookOrderCheckout vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);

        XReturn r = SvcBookOrder.getBean().checkout(vo);
        if (r.ok()) {
            if (vo.getRefund_price().compareTo(BigDecimal.ZERO) > 0) {
                SvcBookOrder.getBean().refundByOrderID(vo.getId());
            }
        }
        return r;
    }
}
